/* linkit.js
 * By François Leporcq
 * MIT Licensed.
 */
var Polygon = AbstractShape.extend({
    _className: "Polygon",
    init: function(points){
        this._points = points;
    },
    setPoints: function(points){
        this._points = points;
    },
    getPoints: function(){
        return this._points;
    },
    setPointAt: function(point, i){
        this._points[i] = point;
    },
    getPointAt: function(i){
        return this._points[i];
    },
    addPointAt: function(point, i){
        this._points.splice(i, 0, point);
    },
    removePointAt: function(i){
        this._points.splice(i, i);
    },
    removePointsBetween: function(i,j){
        this._points.splice(i + 1, j - 1);
    },
    getPointAfter: function(point){
        for(var i = 0 ; i < this._points.length ; i++){
            if(point === this._points[i]){
                return this._points[i+1];
            }
        }
        return null;
    },
    getPointBefore: function(point){
        for(var i = 0 ; i < this._points.length ; i++){
            if(point === this._points[i]){
                return this._points[i-1];
            }
        }
        return null;
    },
    getBox: function(){
        var minX, minY, maxX, maxY = null;
        for(var i =0 ; i < this.getPoints().length ; i++){
            var point = this.getPoints()[i];
            minX = (point.getX() < minX || minX == null) ? point.getX() : minX;
            minY = (point.getY() < minY || minY == null) ? point.getY() : minY;
            maxX = (point.getX() > maxX || maxX == null) ? point.getX() : maxX;
            maxY = (point.getY() > maxY || maxY == null) ? point.getY() : maxY;
        }
        var
        topLeft = new Point(minX,minY),
        topRight = new Point(maxX,minY),
        bottomLeft = new Point(minX,maxY),
        width = topRight.getX() - topLeft.getX(),
        height = bottomLeft.getY() - topRight.getY();
        return new Rectangle(topLeft, width, height);
    },
    getCenter: function(){
        return this.getBox().getCenter();
    },
    getAnchor: function(point){
        return this.getBox().getAnchor(point);
    },
    translate: function(x, y){
        for(var i =0 ; i < this.getPoints().length ; i++){
            this.getPoints()[i].setX(this.getPoints()[i].getX() + x);
            this.getPoints()[i].setY(this.getPoints()[i].getY() + y);
        }
        return this;
    },
    rotate: function(theta,pivot){
        var pivot = (pivot != null ? pivot : new Point(0,0));
        var theta = Math.PI * theta / 180;
        for(var i =0 ; i < this.getPoints().length ; i++){
            var x = this.getPoints()[i].getX();
            var y = this.getPoints()[i].getY();
            this.getPoints()[i].setX(pivot.getX() + (x - pivot.getX()) * Math.cos(theta) - (y - pivot.getY()) * Math.sin(theta));
            this.getPoints()[i].setY(pivot.getY() + (x - pivot.getX()) * Math.sin(theta) + (y - pivot.getY()) * Math.cos(theta));
        }
        return this;
    },
    scale: function(multiplier){
        for(var i =0 ; i < this.getPoints().length ; i++){
            var point = this.getPoints()[i];
            point.setX(point.getX()*multiplier);
            point.setY(point.getY()*multiplier);
        }
    }
}).implement([Linkable]);